package product;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.LineBorder;
import java.awt.SystemColor;

public class DeliveryStock extends JInternalFrame {
	DefaultTableModel model;
	ProductMain4 main;

	private JScrollPane scrollPane;
	private JTable table;
	private JTextField textSearch;
	private JButton btnSearch;
	private JPanel panel;
	private JPanel panel_1;
	private JLabel lblDeliveryStockManagement;

	public DeliveryStock(ProductMain4 main) {
		super("Delivery Stock List", false,true,false,true);
		getContentPane().setBackground(Color.WHITE);
		this.main=main;
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(100, 100, 642, 634);
		getContentPane().setLayout(null);
		getContentPane().add(getScrollPane());
		getContentPane().add(getPanel());
		getContentPane().add(getPanel_1());
		AllSearch();

	}

	public void AllSearch() {
		model.setNumRows(0);
		try {
			String sql = "select code, ea, pro_nm  from delivery_stock inner join code on code=pro_code order by code";

			PreparedStatement pstmt = main.conn.prepareStatement(sql);

			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				Vector<String> vv = new Vector<String>();
				vv.add(rs.getString(1));
				vv.add(rs.getString(3));
				vv.add(rs.getString(2));

				model.addRow(vv);
			}
			rs.close();
			pstmt.close();
		} catch (Exception e) {
		}
	}

	public void Search() {
		boolean flag = true;
		model.setNumRows(0);
		try {
			String sql = "select code, ea, pro_nm  from delivery_stock ds inner join code on code=pro_code "
					+ "where code like ? or pro_nm like ?";

			PreparedStatement pstmt = main.conn.prepareStatement(sql);

			pstmt.setString(1, "%" + textSearch.getText() + "%");
			pstmt.setString(2, "%" + textSearch.getText() + "%");
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				Vector<String> vv = new Vector<String>();
				vv.add(rs.getString(1));
				vv.add(rs.getString(3));
				vv.add(rs.getString(2));

				model.addRow(vv);
				flag = false;
			}
			rs.close();
			pstmt.close();

			if (flag) {
				JOptionPane.showMessageDialog(null, "찾는 데이터가 없습니다.");
			}
		} catch (Exception e) {
		}
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(24, 170, 580, 400);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}

	private JTable getTable() {
		if (table == null) {
			Vector<String> v = new Vector<String>();
			v.add("제품코드");
			v.add("제품이름");
			v.add("재고량");
			model = new DefaultTableModel(v, 0);
			table = new JTable(model);

		}
		return table;
	}

	private JTextField getTextSearch() {
		if (textSearch == null) {
			textSearch = new JTextField();
			textSearch.setText("코드명 또는 품명을 입력하세요.");
			textSearch.setBounds(15, 10, 440, 30);
			textSearch.addKeyListener(new KeyAdapter() {
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.getKeyCode() == KeyEvent.VK_ENTER) {
						if (textSearch.getText().equals("")) {
							AllSearch();
						} else {
							Search();
						}
					}
				}
			});
			textSearch.setColumns(10);
		}
		return textSearch;
	}

	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 15));
			btnSearch.setBounds(460, 10, 100, 30);
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (textSearch.getText().equals("")) {
						AllSearch();
					} else {
						Search();
					}
				}
			});
		}
		return btnSearch;
	}
	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setBackground(SystemColor.activeCaption);
			panel.setBounds(24, 100, 580, 50);
			panel.setLayout(null);
			panel.add(getTextSearch());
			panel.add(getBtnSearch());
		}
		return panel;
	}
	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.setBorder(new LineBorder(Color.GRAY, 2));
			panel_1.setBounds(-21, -21, 702, 104);
			panel_1.add(getLblDeliveryStockManagement());
		}
		return panel_1;
	}
	private JLabel getLblDeliveryStockManagement() {
		if (lblDeliveryStockManagement == null) {
			lblDeliveryStockManagement = new JLabel("Delivery Stock List");
			lblDeliveryStockManagement.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 40));
			lblDeliveryStockManagement.setBounds(144, 31, 360, 61);
		}
		return lblDeliveryStockManagement;
	}
}
